import { reactive } from 'vue'
import { cloneDeep } from 'lodash-es'



/**
 * 构建一个带有重置功能的ref
 *
 * @param defaultValue 默认值
 */
export function useResetableData(
    initialValue
) {
    const origin = cloneDeep(initialValue)
    const finalObj = reactive(initialValue)
    Object.defineProperty(finalObj, '_reset', {
        value: (newV) => {
            if (newV) {
                Object.keys(finalObj).forEach((key) => {
                    finalObj[key] = newV[key] || origin[key]
                })
            } else {
                Object.assign(finalObj, origin)
            }
        }
    })

    return finalObj
}
